package com.ogilvy.audi.webapp.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.ogilvy.audi.model.CircleAcmnt;
import com.ogilvy.audi.model.CircleAlbum;
import com.ogilvy.audi.model.CircleCategory;
import com.ogilvy.audi.service.CircleAlbumManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

public class CircleAlbumAction extends BaseAction implements Preparable {
    private CircleAlbumManager circleAlbumManager;
    private List circleAlbums;
    private CircleAlbum circleAlbum;
    private Long albumId;
    private String query;
    public List<CircleAlbum> albumList; //相册 列表 
	public List<CircleCategory> circleList;//圈子列表
	public List<CircleAcmnt> acmntList;//评论列表
	public List<String> picList;//相片列表 
	public String circleId;
	public CircleAlbum album;
	public File[] image;
	public ServiceDispatcher serviceDispatcher;
    public void setCircleAlbumManager(CircleAlbumManager circleAlbumManager) {
        this.circleAlbumManager = circleAlbumManager;
    }

    public List getCircleAlbums() {
        return circleAlbums;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleAlbumId = getRequest().getParameter("circleAlbum.albumId");
            if (circleAlbumId != null && !circleAlbumId.equals("")) {
                circleAlbum = circleAlbumManager.get(new Long(circleAlbumId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }
//相册 首页
    public String list() {
    	circleId = defValfiledId(circleId);
    	albumList = serviceDispatcher.getCircleAlbumManager().getRecordsByHql("from CircleAlbum where parentId = " +circleId,null);
    	acmntList = serviceDispatcher.getCircleAcmntManager().getRecordsByHql("from CircleAcmnt where albumId = " +albumId,null);
    	return "list";
    }
    public String detail() {
    	album = serviceDispatcher.getCircleAlbumManager().get(albumId);
    	picList = null;//????未实现
    	acmntList = serviceDispatcher.getCircleAcmntManager().getRecordsByHql("from CircleAcmnt where albumId = " +albumId,null);
    	return "list";
    }
    public String showUpLoad(){
		album = serviceDispatcher.getCircleAlbumManager().get(albumId);
		return "upload";
	}
    public String upLoadImg() throws IOException{
		String realPath = ServletActionContext.getServletContext().getRealPath("/images");
		String albumPath = "http://localhost:8080/audisns/albumpic/"+albumId;
		System.out.println(albumPath);
		System.out.println("________________________________________________s"+image);
		   File saveDir = new File(albumPath);
		  
		   if(!saveDir.getParentFile().exists())
		   {
		    saveDir.getParentFile().mkdirs();
		   }
		   File saveFile = null;
		   
		   for(int i = 0;i<image.length;i++)
		   {
		    saveFile = new File(saveDir,"album"+i+".jpeg");
		    FileUtils.copyFile(image[i],saveFile);//将文件保存到硬盘
		    
		   }
		   ActionContext.getContext().put("msg","上传成功");
		   ActionContext.getContext().put("backUrl","/audi/album_showUpload.action?albumId=1");
		   
		   return "audiResult";

		
	}
    public String defValfiledId(String sid){
    	if(sid==null){
    		return "1";
    	}else if(sid.equals("")){
    		return "1";
    	}else{
    		return sid;
    	}
    	
    }

    public void setAlbumId(Long albumId) {
        this.albumId = albumId;
    }

    public CircleAlbum getCircleAlbum() {
        return circleAlbum;
    }

    public void setCircleAlbum(CircleAlbum circleAlbum) {
        this.circleAlbum = circleAlbum;
    }

    public String delete() {
        circleAlbumManager.remove(circleAlbum.getAlbumId());
        saveMessage(getText("circleAlbum.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (albumId != null) {
            circleAlbum = circleAlbumManager.get(albumId);
        } else {
            circleAlbum = new CircleAlbum();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
        	
            return delete();
        }

        boolean isNew = (circleAlbum.getAlbumId() == null);

        circleAlbumManager.save(circleAlbum);

        String key = (isNew) ? "circleAlbum.added" : "circleAlbum.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}